<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="名称" prop="name">
                <el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="编号" prop="number">
                <el-input v-model="queryParams.number" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="ip地址" prop="ipAddr">
                <el-input v-model="queryParams.ipAddr" placeholder="请输入ip地址" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="端口" prop="port">
                <el-input v-model="queryParams.port" placeholder="请输入端口" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
                    <el-option v-for="dict in dict.type.basic_asset_status_dict" :key="dict.value" :label="dict.label" :value="dict.value"/>
                </el-select>
            </el-form-item>
            <el-form-item label="单价" prop="price">
                <InputNumberRange size="mini" controls_position="right" :value="priceRange"/>
            </el-form-item>
            <el-form-item label="采购日期">
                <el-date-picker v-model="daterangePurchaseTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" clearable/>
            </el-form-item>
            <el-form-item label="厂商" prop="vendor">
                <el-input v-model="queryParams.vendor" placeholder="请输入厂商" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="厂商电话" prop="vendorTel">
                <el-input v-model="queryParams.vendorTel" placeholder="请输入厂商电话" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="存放地点" prop="place">
                <el-select v-model="queryParams.place" filterable allow-create default-first-option @keyup.enter.native="handleQuery" placeholder="请选择存放地点" clearable>
                    <el-option v-for="item in ex_placeList" :key="item.name" :label="item.name" :value="item.name"/>
                </el-select>
            </el-form-item>
            <el-form-item label="保修日期">
                <el-date-picker v-model="daterangeWarrantyDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" clearable/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                           v-hasPermi="['basic:asset_list:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                           v-hasPermi="['basic:asset_list:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                           @click="handleDelete" v-hasPermi="['basic:asset_list:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" plain icon="el-icon-upload" size="mini" @click="handleImport" v-hasPermi="['basic:asset_list:import']">导入</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                           v-hasPermi="['basic:asset_list:export']">导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="asset_listList" @selection-change="handleSelectionChange" :height="table_height">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="名称" align="center" prop="name"/>
            <el-table-column label="编号" align="center" prop="number"/>
            <el-table-column label="ip地址" align="center" prop="ipAddr"/>
            <el-table-column label="端口" align="center" prop="port"/>
            <el-table-column label="状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.basic_asset_status_dict" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="存放地点" align="center" prop="place"/>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['basic:asset_list:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['basic:asset_list:remove']">删除</el-button>
                </template>
            </el-table-column>
            <el-table-column type="expand">
                <template slot-scope="scope">
                    <el-descriptions :column="5" size="mini" border>
                        <el-descriptions-item label-class-name="my-label" label="厂商">{{scope.row.vendor}}</el-descriptions-item>
                        <el-descriptions-item label-class-name="my-label" label="厂商电话">{{scope.row.vendorTel}}</el-descriptions-item>
                        <el-descriptions-item label-class-name="my-label" label="单价">{{scope.row.price}}</el-descriptions-item>
                        <el-descriptions-item label-class-name="my-label" label="采购日期">
                            {{ parseTime(scope.row.purchaseTime, '{y}-{m}-{d}') }}
                        </el-descriptions-item>
                        <el-descriptions-item label-class-name="my-label" label="保修日期">
                            {{ parseTime(scope.row.warrantyDate, '{y}-{m}-{d}') }}
                        </el-descriptions-item>
                    </el-descriptions>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>

        <!-- 添加或修改资产管理对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="form.name" placeholder="请输入名称" class="input-style" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="ip地址" prop="ipAddr">
                            <el-input v-model="form.ipAddr" placeholder="请输入ip地址" class="input-style" clearable/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="编号" prop="number">
                            <el-input v-model="form.number" placeholder="请输入编号" class="input-style" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="端口" prop="port">
                            <el-input-number v-model="form.port" placeholder="请输入端口" class="input-style" clearable/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="form.type" placeholder="请选择类型" class="input-style" disabled>
                                <el-option v-for="dict in dict.type.basic_asset_type_dict" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="单价" prop="price">
                            <template>
                                <el-input-number v-model="form.price" :precision="2" :step="1" class="input-style"/>
                            </template>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="form.status" placeholder="请选择状态" class="input-style" clearable>
                                <el-option v-for="dict in dict.type.basic_asset_status_dict" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="采购日期" prop="purchaseTime">
                            <el-date-picker clearable v-model="form.purchaseTime" type="date" value-format="yyyy-MM-dd" class="input-style" placeholder="请选择采购日期" clearable/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="厂商" prop="vendor">
                            <el-input v-model="form.vendor" placeholder="请输入厂商" class="input-style" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="厂商电话" prop="vendorTel">
                            <el-input v-model="form.vendorTel" placeholder="请输入厂商电话" class="input-style" clearable/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="保修日期" prop="warrantyDate">
                            <el-date-picker clearable v-model="form.warrantyDate" type="date" value-format="yyyy-MM-dd" class="input-style" placeholder="请选择保修日期" clearable/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="存放地点" prop="place">
                            <el-select v-model="form.place" filterable allow-create default-first-option placeholder="请选择存放地点" clearable>
                                <el-option v-for="item in ex_placeList" :key="item.name" :label="item.name" :value="item.name"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="21">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" type="textarea" :rows="3" maxlength="300" show-word-limit placeholder="请输入备注"/>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 教师导入对话框 -->
        <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
            <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport"
                       :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <div class="el-upload__tip text-center" slot="tip">
                    <span>仅允许导入xls、xlsx格式文件。</span>
                    <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {listAsset_list, getAsset_list, delAsset_list, addAsset_list, updateAsset_list} from "@/api/basic/asset_list";
import InputNumberRange from "@/views/components/number/input-number-range.vue";
import {copyObj, isNotNull} from "@/utils/zzDouUtils/objUtil";
import {isEmptyArr} from "@/utils/zzDouUtils/arrayUtil";
import {listEx_place} from "@/api/basic/ex_place";
import {getToken} from "@/utils/auth";
import {nowDateStr} from "@/utils/zzDouUtils/dateUtil";
export default {
    name: "Asset_list",
    dicts: ['basic_asset_type_dict', 'basic_asset_status_dict'],
    components:{InputNumberRange},
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 资产管理表格数据
            asset_listList: [],
            // 场地数据
            ex_placeList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            //价格范围
            priceRange: [],
            // 时间范围
            daterangePurchaseTime: [],
            // 时间范围
            daterangeWarrantyDate: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                name: null,
                number: null,
                ipAddr: null,
                port: null,
                type: "1",
                status: null,
                price: null,
                purchaseTime: null,
                vendor: null,
                vendorTel: null,
                place: null,
                warrantyDate: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                name: [
                    {required: true, message: "名称不能为空", trigger: "blur"}
                ],
                number: [
                    {required: true, message: "编号不能为空", trigger: "blur"}
                ],
                ipAddr: [
                    {required: true, message: "ip地址不能为空", trigger: "blur"}
                ],
                port: [
                    {required: true, message: "端口不能为空", trigger: "blur"}
                ],
                // status: [
                //     {required: true, message: "状态不能为空", trigger: "change"}
                // ],
                // price: [
                //     {required: true, message: "单价不能为空", trigger: "blur"}
                // ],
                // purchaseTime: [
                //     {required: true, message: "采购日期不能为空", trigger: "blur"}
                // ],
                // vendor: [
                //     {required: true, message: "厂商不能为空", trigger: "blur"}
                // ],
                // vendorTel: [
                //     {required: true, message: "厂商电话不能为空", trigger: "blur"}
                // ],
                // place: [
                //     {required: true, message: "存放地点不能为空", trigger: "blur"}
                // ],
                // warrantyDate: [
                //     {required: true, message: "保修日期不能为空", trigger: "blur"}
                // ],
                // remark: [
                //     {required: true, message: "备注不能为空", trigger: "blur"}
                // ],
            },
            // 导入参数
            upload: {
                // 是否显示弹出层
                open: false,
                // 弹出层标题
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 设置上传的请求头部
                headers: {Authorization: "Bearer " + getToken()},
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/basic/asset_list/import/camera"
            },
        };
    },
    created() {
        this.getList();
        this.getPlaceList();
    },
    methods: {
        /** 查询考站表列表 */
        getPlaceList() {
            listEx_place({pageSize:1000,pageNum:1}).then(response => {
                this.ex_placeList = response.rows;
            });
        },
        /** 查询资产管理列表 */
        getList() {
            this.loading = true;
            this.queryParams.params = {};
            if (isNotNull(this.priceRange) && !isEmptyArr(this.priceRange)) {
                this.queryParams.params["beginPrice"] = this.priceRange[0];
                this.queryParams.params["endPrice"] = this.priceRange[1];
            }
            if (null != this.daterangePurchaseTime && '' != this.daterangePurchaseTime) {
                this.queryParams.params["beginPurchaseTime"] = this.daterangePurchaseTime[0];
                this.queryParams.params["endPurchaseTime"] = this.daterangePurchaseTime[1];
            }
            if (null != this.daterangeWarrantyDate && '' != this.daterangeWarrantyDate) {
                this.queryParams.params["beginWarrantyDate"] = this.daterangeWarrantyDate[0];
                this.queryParams.params["endWarrantyDate"] = this.daterangeWarrantyDate[1];
            }
            listAsset_list(this.queryParams).then(response => {
                this.asset_listList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                name: null,
                number: null,
                ipAddr: null,
                port: null,
                type: 1,
                status: null,
                price: null,
                purchaseTime: null,
                vendor: null,
                vendorTel: null,
                place: null,
                warrantyDate: null,
                remark: null,
                reservedOne: null,
                reservedTwo: null,
                createTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.daterangePurchaseTime = [];
            this.daterangeWarrantyDate = [];
            this.priceRange = [];
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加资产管理";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.form = copyObj(row)
            this.open = true;
            this.title = "修改资产管理";
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateAsset_list(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAsset_list(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除资产管理编号为"' + ids + '"的数据项？').then(function () {
                return delAsset_list(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('basic/asset_list/export', {...this.queryParams}, `摄像头管理_${nowDateStr()}.xlsx`)
        },
        /** 导入按钮操作 */
        handleImport() {
            this.upload.title = "资产导入";
            this.upload.open = true;
        },
        /** 下载模板操作 */
        importTemplate() {
            this.download('basic/asset_list/export-template/camera', {}, `摄像头管理模板_${nowDateStr()}.xlsx`)
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
            this.getList();
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
    }
};
</script>
<style>
.my-label{
    font-size: 17px;
    font-weight: bold;
    background: green;
    width: 150px;
}
.input-style {
    width: 220px;
}
</style>
